package com.woniuxy.usercoachassemble.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.gson.internal.$Gson$Types;
import com.woniuxy.usercoachassemble.commons.QiNiuUtils;
import com.woniuxy.usercoachassemble.commons.ToData;
import com.woniuxy.usercoachassemble.entity.*;
import com.woniuxy.usercoachassemble.service.*;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-07-18
 */
@RestController
@RequestMapping("yoga-gym-info")
public class YogaGymInfoController {

    @Autowired
    private CoachInfoService coachInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private YogaGymInfoService yogaGymInfoService;
    @Autowired
    private YogaGymImgService yogaGymImgService;

    @Autowired
    private YogaGymSignedCoachService yogaGymSignedCoachService;
    @Autowired
    private CoachCourseService coachCourseService;
    @Autowired
    private CourseService courseService;
    /**
     * @description:TODO(场馆动态条件查询教练)
     * @param nickname
     * @param username
     * @param tel
     * @param current
     * @param size
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/18 15:00
     */
    @GetMapping("selectCoachInfoByDynamic")
    public Result selectCoachInfoByDynamic(String nickname,String username,String tel,Long current,Long size){

        ToData toData = coachInfoService.selectCoachInfoByDynamic(nickname, username, tel, current, size);
        return Result.Success(toData);
    }
    //个人中心start

    @GetMapping("getAuth")
    public Result getAuth(HttpServletRequest request){
        int userId= Integer.parseInt(request.getHeader("userId"));
        QueryWrapper<YogaGymInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        YogaGymInfo one = yogaGymInfoService.getOne(wrapper);
        return Result.Success(one);
    }
    @GetMapping("selectMoney")
    public Result selectAccountMoney(HttpServletRequest request){
        int userId= Integer.parseInt(request.getHeader("userId"));
        Account account = coachInfoService.selectAccount(userId);

        return Result.Success(account);
    }
    @PostMapping("setMoney")
    public Result selectAccountMoney(HttpServletRequest request, String accountName){
        int userId= Integer.parseInt(request.getHeader("userId"));
        coachInfoService.insertAccountMoney( userId,  accountName);
        return Result.Success();
    }

    @GetMapping("getCoachByUserId")
    public Result getCoachBy(HttpServletRequest request){
        int userId= Integer.parseInt(request.getHeader("userId"));

        List<Map<String, Object>> coach = yogaGymSignedCoachService.getCoachByUserId(userId);

        return Result.Success(coach);
    }
    @GetMapping("getCourseByCoach")
    public Result getCoachByCoach(int CoachId){

        List<Map<String, Object>> course = yogaGymSignedCoachService.getCoachByCoach(CoachId);

        return Result.Success(course);
    }
    @GetMapping("getTimeByCoach")
    public Result getTimeByCoach(int CoachId) {

        List<Map<String, Object>> time = yogaGymSignedCoachService.getTimeByCoach(CoachId);

        return Result.Success(time);
    }
    @RequestMapping(value = "insertFile",method = RequestMethod.POST)
    public Result insertFile(List<MultipartFile> files, HttpServletRequest request) throws Exception {

        if(files.size()==0){
            return Result.Error();
        }
        for (MultipartFile f:files) {
            String uuid=UUID.randomUUID().toString().replace("-","");
            try {
                String success = QiNiuUtils.uploadQNImg(f.getInputStream(), uuid);
            } catch (IOException e) {

                e.printStackTrace();
                return Result.Error();
            }
            //图片地址
            String img = QiNiuUtils.fullPath(uuid);
            YogaGymImg yogaGymImg = new YogaGymImg();
            yogaGymImg.setImg(img);

            yogaGymImg.setUserId(Integer.parseInt(request.getHeader("userId")));
            yogaGymImgService.save(yogaGymImg);
        }

        return Result.Success();
    }
    //个人中心end
    //完善信息start
    @RequestMapping(value = "insertGymInFo",method = RequestMethod.POST)
    public Result insertGymInFo(HttpServletRequest request,Userinfo userinfo,YogaGymInfo yogaGymInfo,List<MultipartFile> files){


        if(files.size()==0){
            return Result.Error();
        }
        for (MultipartFile f:files) {
            String uuid=UUID.randomUUID().toString().replace("-","");
            try {
                String success = QiNiuUtils.uploadQNImg(f.getInputStream(), uuid);
            } catch (IOException e) {

                e.printStackTrace();
                return Result.Error();
            }
            //图片地址
            String img = QiNiuUtils.fullPath(uuid);
            YogaGymImg yogaGymImg = new YogaGymImg();
            yogaGymImg.setImg(img);

            yogaGymImg.setUserId(Integer.parseInt(request.getHeader("userId")));
            yogaGymImgService.save(yogaGymImg);
        }
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",Integer.parseInt(request.getHeader("userId")));
        Userinfo userinfo1 = userinfoService.getOne(wrapper);
        userinfo1.setNickname(userinfo.getNickname());
        userinfo1.setRealName(userinfo.getRealName());
        userinfoService.updateById(userinfo1);
        YogaGymInfo yogaGymInfo1 = new YogaGymInfo();
        QueryWrapper<YogaGymInfo> yogaGymInfoQueryWrapper = new QueryWrapper<>();
        yogaGymInfoQueryWrapper.eq("user_id", Integer.parseInt(request.getHeader("userId")));
        YogaGymInfo one = yogaGymInfoService.getOne(yogaGymInfoQueryWrapper);
        if(one!=null) {
            yogaGymInfo1.setYogaGymId(one.getYogaGymId());
        }
        yogaGymInfo1.setUserId(Integer.parseInt(request.getHeader("userId")));
        yogaGymInfo1.setPhone(yogaGymInfo.getPhone());
        yogaGymInfo1.setYogaGymName(yogaGymInfo.getYogaGymName());
        yogaGymInfo1.setYogaGymDetails(yogaGymInfo.getYogaGymDetails());
        yogaGymInfo1.setA1(2);
        yogaGymInfoService.saveOrUpdate(yogaGymInfo1);
        User user = new User();
        user.setA(2);
        user.setUserId(Integer.parseInt(request.getHeader("userId")));
        userService.saveOrUpdate(user);
        return Result.Success();
    }
    @GetMapping("getGymImg")
    public Result getGymImg(HttpServletRequest request) {
        int userId= Integer.parseInt(request.getHeader("userId"));
        QueryWrapper<YogaGymImg> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<YogaGymImg> list = yogaGymImgService.list(wrapper);
        return Result.Success(list);
    }
    @GetMapping("authCoachByGym")
    public Result authCoachByGym(HttpServletRequest request,int CoachId) {
        int userId= Integer.parseInt(request.getHeader("userId"));
        UpdateWrapper<CoachInfo> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",CoachId);
        CoachInfo coachInfo = new CoachInfo();
        coachInfo.setIndentification(2);
        coachInfoService.update(coachInfo,wrapper);
        return Result.Success();
    }




}

